# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd
import xgboost as xgb

import scipy as sp

def logloss(act, pred):
  epsilon = 1e-15
  pred = sp.maximum(epsilon, pred)
  pred = sp.minimum(1-epsilon, pred)
  ll = sum(act*sp.log(pred) + sp.subtract(1,act)*sp.log(sp.subtract(1,pred)))
  ll = ll * -1.0/len(act)
  return ll

def loadCSV(path=''):
    return pd.read_csv(path)
    
#训练分类器XGB
def XGB(x_train,y_train,f_names=None,buffer=False):
    
    print('使用XGBOOST进行训练')
    if buffer==True:
        dtrain=xgb.DMatrix("data/train.buffer")
    else:
        dtrain=xgb.DMatrix(x_train,label=y_train,feature_names=f_names)
        dtrain.save_binary("data/train.buffer")
    
    param = {'max_depth':7,
             'eta':0.25,
             'min_child_weight':1, 
             'silent':1,
             'subsample':1,
             'colsample_bytree':1,
             'gamma':0,
             'scale_pos_weight':1,
             'lambda':50,
             'objective':'binary:logistic'}

    plst = list(param.items())
    plst += [('eval_metric', 'auc')]  
    plst += [('eval_metric', 'ams@0')]
    
    num_round = 200
    bst = xgb.train(plst,dtrain,num_boost_round=num_round)
  
    return bst
    
def predict_test_prob(bst):
    df_all=loadCSV('data/cutData/test_v6.csv') 
    
    df_sta_lgbm=loadCSV('data/pro/pro_lgbm_test.csv') 
    print('开始拼接')
    df_all=pd.merge(df_all,df_sta_lgbm,how='left',on='instanceID')
    del df_sta_lgbm
    
    instanceID=df_all.instanceID.values
    feature_all=df_all.drop(['label','clickTime','instanceID','sort',
                             'appCategory'],axis=1).values
                             
    del df_all
                             
    dtest=xgb.DMatrix(feature_all)
    prob=bst.predict(dtest)
    
    output=pd.DataFrame({'instanceID':instanceID,'prob':prob})
    
    output.to_csv('result/submission2.csv',index=False)  
    
#主函数入口
if __name__=='__main__':
  
   df_all=loadCSV('data/cutData/feature_v6.csv') 
   df_sta_lgbm=loadCSV('data/pro/pro_lgbm_train.csv') 
   print('开始拼接')
   df_all=pd.merge(df_all,df_sta_lgbm,how='left',on='sort')
   del df_sta_lgbm
   
   feature_all=df_all.drop(['sort','label','clickTime','conversionTime',
                            'appCategory'],axis=1).values                         
                            
   label_all=df_all.label.values
   
   del df_all
   
   bst=XGB(feature_all,label_all)
   print('分类器训练完成')
   predict_test_prob(bst)
   print('结果预测完成')








   
